# AMAR ET AL. - COUNTERING MISINFORMATION EARLY (2025)
## REPLICATION FILE: 17_effect_in_median_shift.R
### This script estimates the effect of the treatment on the median of the index of indices, comparing it to the control group.
# ----
# Cost effectiveness ----
# Subset to treatment groups
control_vals <- bimli %>%
  filter(treatment == "Spoken English") %>%
  pull(index_of_indices)

treatment_vals <- bimli %>%
  filter(treatment == "Media Literacy") %>%
  pull(index_of_indices)

# Compute medians
control_median <- median(control_vals, na.rm = TRUE)
treatment_median <- median(treatment_vals, na.rm = TRUE)

# Compute quantile of treatment median in control group
quantile_position <- ecdf(control_vals)(treatment_median)

# Create plot
ggplot(data = data.frame(control_vals), aes(x = control_vals)) +
  geom_density(fill = "lightgray", alpha = 0.7) +
  geom_vline(xintercept = control_median, linetype = "dashed", color = "black", size = 1) +
  geom_vline(xintercept = treatment_median, linetype = "dotted", color = "red", size = 1) +
  annotate("text", x = control_median, y = 0.05, label = paste0("Control median = ", round(control_median, 2)),
           vjust = -1, hjust = 0, angle = 90) +
  annotate("text", x = treatment_median, y = 0.05, label = paste0("Treatment median = ", round(treatment_median, 2)),
           vjust = -1, hjust = 0, angle = 90, color = "red") +
  labs(title = "Distribution of Index of Indices (Control Group)",
       subtitle = paste0("Treatment shifts the median student from the 50th to the ",
                         round(quantile_position * 100, 1), "st percentile of the control distribution"),
       x = "Index of Indices", y = "Density") +
  theme_bw() +
  xlim(-3,3) +
  ylim(0,0.45)
ggsave("output/figures/median_shift_effect.pdf", height = 5, width = 8)

# END of 17_effect_in_median_shift.R ----
